package cn.ibobo.lease.admin.service.impl;

import cn.ibobo.lease.admin.dao.UserMapper;
import cn.ibobo.lease.admin.dao.UserPointMapper;
import cn.ibobo.lease.admin.service.UserPointService;
import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.common.dto.UserDTO;
import cn.ibobo.lease.common.dto.UserPointDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.DateUtil;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;


/**
 * 用户积分记录表业务处理接口
 * @Author lililiang
 * @Date 2019-02-17 12:57:38
 */
@Service
public class UserPointServiceImpl extends BaseServiceImpl<UserPointDTO, String> implements UserPointService {

    private static final Log LOGGER = LogFactory.getLog(UserPointServiceImpl.class);

    @Autowired
    private UserPointMapper mapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    protected BaseMapper<UserPointDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 用户积分记录表列表
     * @Author lililiang
     * @Date 2019-02-17 12:57:38
     * @Param
     * @Return
     */
    @Override
    public List<UserPointDTO> list4Page(UserPointDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 用户积分记录表列表（无分页）
     * @Author lililiang
     * @Date 2019-02-17 12:57:38
     * @Param
     * @Return
     */
    @Override
    public List<UserPointDTO> listByCondition(UserPointDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询用户积分记录表详情信息
     * @Author lililiang
     * @Date 2019-02-17 12:57:38
     * @Param
     * @Return
     */
    @Override
    public UserPointDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存用户积分记录表信息
     * @Author lililiang
     * @Date 2019-02-17 12:57:38
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(UserPointDTO entity) {
        return super.save(entity);
    }

    /**
     * 删除用户积分记录表信息
     * @Author lililiang
     * @Date 2019-02-17 12:57:38
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }

    /**
     * @Description 增加/减少用户积分
     * @Author lililiang
     * @Date 2019/2/17 13:11
     * @Param [entityDTO]
     * @Return java.lang.Integer
     */
    @Override
    @Transactional
    public Integer point(UserPointDTO entityDTO){
        //1. 根据类型修改用户表中的积分/总积分数量
        UserDTO user = new UserDTO();
        if(entityDTO.getType() == 1){
            user.setType((byte)2);//减少积分
        }else{
            user.setType((byte)1);//增加积分
        }
        user.setPoint(entityDTO.getPoint());
        user.setUid(entityDTO.getUid());
        if(userMapper.point(user) <= 0){
            return ResultEnum.FAIL.getCode();
        }
        //2. 新增用户积分记录
        entityDTO.setId(UuidUtil.getUuid());
        entityDTO.setCreatetm(DateUtil.getNow());
        if(mapper.add(entityDTO) <= 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultEnum.FAIL.getCode();
        }
        return ResultEnum.SUCCESS.getCode();

    }
}
